package com.android.filter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.android.filter.Frame.FrameInterface;
import com.android.filter.FrameStatus.ChangeStatusCallback;
import com.example.plasma.R;

public class Mosaic extends FrameStatus implements FrameInterface{

	public Mosaic(Context context)
	{
		super();
		mContext = context;
		mGridViewPager = (ViewPager) ((Activity)mContext).findViewById(R.id.mosaic_view_pager);
		mMosaicView = (MosaicView) ((Activity)mContext).findViewById(R.id.mosaic_view);
		
	}

	public void destroy() {
		if(mMosaicView != null)
			mMosaicView.destroy();
		if(mGridViewPager != null)
			mGridViewPager.setAdapter(null);
		if(mViewPagerAdapter != null)
			mViewPagerAdapter.destroy();
	}

	public void configurationChanged() {
		// TODO Auto-generated method stub
		
	}
	
	public void save()
	{
		String name = mMosaicView.getFileName();
		name = name.substring(0, name.lastIndexOf("."));
		String saveFileName =  name + "_mosaic.png";
		mViewPagerAdapter.save(mContext.getContentResolver(), mMosaicView.getFilePath(), saveFileName);
	}
	
	public void setMosaicViewNUri(Uri uri)
	{
		if(mMosaicView != null)
			mMosaicView.initView(uri);
		mViewPagerAdapter = new MosaicViewPagerAdapterClass(mContext);
		int width = mMosaicView.getDrawBitmapWidth();
		int height = mMosaicView.getDrawBitmapHeight();
		mViewPagerAdapter.init(width, height);
		mGridViewPager.setAdapter(mViewPagerAdapter);
		mGridViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
		mGridViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
			}
			
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				mPageViewPosition = arg0;
			}
			
			public void onPageScrollStateChanged(int arg0) {
				if(arg0 == ViewPager.SCROLL_STATE_IDLE)
				{
					mViewPagerAdapter.resetOther(mPageViewPosition, mMosaicView);
				}
				else if(arg0 == ViewPager.SCROLL_STATE_DRAGGING)
				{
					mViewPagerAdapter.startDrag(mMosaicView);
				}
			}
		});
		mViewPagerAdapter.resetOther(mPageViewPosition, mMosaicView);
	}
	public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
	    private static final float MIN_SCALE = 0.85f;
	    private static final float MIN_ALPHA = 0.5f;

		public void transformPage(View view, float position) {
			int pageWidth = view.getWidth();
	        int pageHeight = view.getHeight();

	        if (position < -1) { // [-Infinity,-1)
	            // This page is way off-screen to the left.
	            view.setAlpha(0);

	        } else if (position <= 1) { // [-1,1]
	            // Modify the default slide transition to shrink the page as well
	            float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
	            float vertMargin = pageHeight * (1 - scaleFactor) / 2;
	            float horzMargin = pageWidth * (1 - scaleFactor) / 2;
	            if (position < 0) {
	                view.setTranslationX(horzMargin - vertMargin / 2);
	            } else {
	                view.setTranslationX(-horzMargin + vertMargin / 2);
	            }

	            // Scale the page down (between MIN_SCALE and 1)
	            view.setScaleX(scaleFactor);
	            view.setScaleY(scaleFactor);

	            // Fade the page relative to its size.
	            view.setAlpha(MIN_ALPHA +
	                    (scaleFactor - MIN_SCALE) /
	                    (1 - MIN_SCALE) * (1 - MIN_ALPHA));

	        } else { // (1,+Infinity]
	            // This page is way off-screen to the right.
	            view.setAlpha(0);
	        }
		}
	}
	public void setChangeStatusCallback(ChangeStatusCallback callback) {
		mChangeStatusCallback = callback;
	}
	
	public void backPressed() {
		mChangeStatusCallback.changeStatus(Frame.LAUNCHER);
	}
	private int mPageViewPosition = 0;
	private MosaicViewPagerAdapterClass mViewPagerAdapter = null;
	private Context mContext = null;
	private MosaicView mMosaicView = null;
	private ViewPager mGridViewPager = null;
}
